//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    showScanImage: false,
    imageURL: "",
    setIntervalFn: null,
    num: 0, // 奖金
    ranking: 0, // 排名
    score: 0, // 得分
    allNum: 0, //奖池
    ratio: 0,
    allPeople: 0,
    isShowStatistics: false,
  },
  // 工资单
  wage() {
    if (global.$cacheHelper.appUser() == null) {
      global.$cacheHelper.tologin()
    } else {
      wx.navigateTo({
        url: '../index/wageodd/wageodd'
      })
    }
  },

  //开始生产
  process() {
    if (global.$cacheHelper.appUser() == null) {
      global.$cacheHelper.tologin()
    } else {
      //跳转扫描
      wx.navigateTo({
        url: './scan/scan?type=10&process=null',
      })
    }
  },

  //结束生产
  overproduction() {
    if (global.$cacheHelper.appUser() == null) {
      global.$cacheHelper.tologin()
    } else {
      wx.navigateTo({
        url: './overproduction/overproduction',
      })
    }
  },
  //整件生产
  wholePieceproduction() {
    if (global.$cacheHelper.appUser() == null) {
      global.$cacheHelper.tologin()
    } else {
      wx.navigateTo({
        url: './wholePieceproduction/wholePieceproduction',
      })
    }
  },
  //生产记录
  record() {
    if (global.$cacheHelper.appUser() == null) {
      global.$cacheHelper.tologin()
    } else {
      wx.navigateTo({
        url: './record/record',
      })
    }
  },

  //工资单
  payroll() {
    if (global.$cacheHelper.appUser() == null) {
      global.$cacheHelper.tologin()
    } else {
      wx.navigateTo({
        url: './payroll/payroll',
      })
    }
  },
  async getScanImage() {
    var data = await global.$api.post({
      url: 'api/userInfo/getAttendanceQrCode',
      data: {}
    })
    return "data:image/png;base64," + data.data
  },
  //获取几秒调用一次接口 刷新时间
  async getRefreshTime() {
    var data = await global.$api.post({
      url: 'api/userInfo/getCode',
      data: {
        code: "qr_code_failure_time"
      }
    })
    return data.data;
  },
  clearTimer() {
    console.log("进入清除定时器缓存");
    if (this.data.setIntervalFn) {
      clearInterval(this.data.setIntervalFn)
      this.data.setIntervalFn = null
    }
  },
  onHide: function () {
    this.clearTimer();
    this.setData({
      showScanImage: false,
      imageURL: "",
      setIntervalFn: null
    })
  },
  onUnLoad: function () {
    this.clearTimer();
    this.setData({
      showScanImage: false,
      imageURL: "",
      setIntervalFn: null
    })
  },
  onShow: async function () {
    this.clearTimer();
    // 总体统计
    this.getStatistics();
    let userInfo = JSON.parse(wx.getStorageSync('app_user_info'));
    if (userInfo.isAttendance == 2) {
      // let qr_code_failure_time = await this.getRefreshTime();
      let imageURL = await this.getScanImage();
      this.setData({
        showScanImage: userInfo.isAttendance == 2 ? true : false,
        imageURL: imageURL
      })
      this.data.setIntervalFn = setInterval(async () => {
        let imageURL = await this.getScanImage();
        this.setData({
          showScanImage: userInfo.isAttendance == 2 ? true : false,
          imageURL: imageURL
        })
      }, 8000);
      // (qr_code_failure_time || 10) * 1000
    }
  },
  onLoad: async function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      // wx.getUserInfo({
      //   success: res => {
      //     app.globalData.userInfo = res.userInfo
      //     this.setData({
      //       userInfo: res.userInfo,
      //       hasUserInfo: true
      //     })
      //   }
      // })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  getStatistics() {
    // 总体统计
    global.$api.post({
      url: 'api/userInfo/statistics',
      data: {}
    }).then(res => {
      console.log(1111111,res);
      if (res.code == 501) {
        this.setData({
          isShowStatistics: false
        })
      } else {
        this.setData({
          num: res.data.num,
          ranking: res.data.ranking,
          score: res.data.score,
          ratio: res.data.ratio,
          allNum: res.data.allNum,
          allPeople: res.data.allPeople,
          isShowStatistics: true
        })
      }

    })
  },
  showStatistics() {
    // 跳转到5个纬度统计页面
    if (global.$cacheHelper.appUser() == null) {
      global.$cacheHelper.tologin()
    } else {
      wx.navigateTo({
        url: './statistics/statistics',
      })
    }
  }
})